﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/*
隐身
 */
public class CloakDevice : Weapon {

	private float maxBuffer;//最大缓冲
	private float currBuffer;//当前缓冲
	private float everyBuffer;//每帧缓冲
	private float expendBuffer;//每帧消耗
	private float tempBuffer;//缓冲，用于恢复和消耗

	private GameObject target;
	private GameObject gun;
	private List<GameObject> shields;//作用目标下的护盾
	private bool isUsing = false;//当前装置是否作用
	private List<GameObject> guns;//作用目标下的所有枪


	

	// Use this for initialization
	void Start () {
		Init();
		AttackTargetManager.SendMsgToDevice += ChangeState;
	}
	
	// Update is called once per frame
	void Update () {
		Cloak();
		RecoverAndExpendBuffer();
		if(currBuffer <= 0 && isUsing)
		{
			isUsing = false;
			target.transform.position = new Vector3(target.transform.position.x, target.transform.position.y, target.transform.position.z + 100);
			
		}
	}

	private void Init()
	{
		maxBuffer = 1000;
		currBuffer = maxBuffer;
		everyBuffer = 1;
		expendBuffer = 2;

		shields = new List<GameObject>();
		guns = new List<GameObject>();
		if(target.transform.Find("Gun (0)") != null){guns.Add(target.transform.Find("Gun (0)").gameObject);};
		if(target.transform.Find("Gun (1)") != null){guns.Add(target.transform.Find("Gun (1)").gameObject);};
		if(target.transform.Find("Gun (2)") != null){guns.Add(target.transform.Find("Gun (2)").gameObject);};
		if(target.transform.Find("Gun (3)") != null){guns.Add(target.transform.Find("Gun (3)").gameObject);};
		if(target.transform.Find("Gun (4)") != null){guns.Add(target.transform.Find("Gun (4)").gameObject);};
		if(target.transform.Find("Gun (5)") != null){guns.Add(target.transform.Find("Gun (5)").gameObject);};
	}

	private void Cloak()
	{
		if(target != null)
		{
			
			// 隐形
			if(isUsing)
			{
				print("隐身");
				// 敌机无法攻击
				// 遍历所有子物体得到所有护盾
				foreach(Transform child in target.transform)
				{
					child.position = new Vector3(child.transform.position.x, child.transform.position.y, target.transform.position.z); 
				}
				// 自己无法攻击
				foreach (GameObject gun in guns)
				{
					gun.GetComponent<PlayerAttack>().isCloaking = true;
				}
			}
			// 现型
			else
			{
				// 敌机可以攻击
				// 自己无法攻击
				foreach (GameObject gun in guns)
				{
					gun.GetComponent<PlayerAttack>().isCloaking = false;
				}
			}
		}
		
	}



	public void SetGun(GameObject gun)
	{
		this.gun = gun;
	}

	public void SetTarget(GameObject target)
	{
		this.target = target;
	}

	public void SetIsUsing(bool isUsing)
	{
		this.isUsing = isUsing;
	}

	public void RecoverAndExpendBuffer()
	{
		if(!isUsing)
		{
			tempBuffer = currBuffer + everyBuffer;
			if(tempBuffer < maxBuffer)
			{
				currBuffer = tempBuffer;
			}
			else
			{
				currBuffer = maxBuffer;
			}
			tempBuffer = 0;
		}
		else
		{
			tempBuffer = currBuffer - expendBuffer;
			if(tempBuffer > 0)
			{
				currBuffer = tempBuffer;
			}
			else
			{
				currBuffer = 0;
			}
			tempBuffer = 0;
		}
	}

	private void ChangeState(string weaponName, string gunName)
	{
		if(weaponName == "CloakDevice" && gunName == gun.gameObject.name)
		{
			print("接收到了");
			if(isUsing)
			{
				isUsing = false;
				target.transform.position = new Vector3(target.transform.position.x, target.transform.position.y, target.transform.position.z + 100);				
			}
			else
			{
				isUsing = true;
				target.transform.position = new Vector3(target.transform.position.x, target.transform.position.y, target.transform.position.z - 100);				
			}
		}
	}
	

}
